package com.ithong.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ithong.drug.entity.Problem;
import com.ithong.drug.service.ProblemService;
import com.ithong.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    /**
     * 转向药品信息页面
     * @return
     */
    @RequestMapping
    public String index(){
        return "/problem";
    }

    /**
     * 分页查询药品信息列表
     */
    @RequestMapping("page")
    @ResponseBody
    public Object problemQueryPage(String param,
                                    @RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize){
        try {
            IPage<Problem> iPage = problemService.selectproblemPage(pageNum,pageSize,param);
            return ResultMapUtil.getMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 转向新增页面
     * @return
     */
    @RequestMapping("add")
    public String add(){
        return "/problemAdd";
    }

    /**
     * 添加一个药品信息
     */
    @RequestMapping("/problemAdd")
    @ResponseBody
    public Object problemAdd(Problem problem){
        try {
            problem.setCreatetime(new Date());
            int i = problemService.addproblem(problem);
            return ResultMapUtil.getMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 转向编辑页面
     * @return
     */
    @RequestMapping("edit")
    public String edit(@RequestParam(name = "id",required = true) Integer id, Model model){
        Problem problem = problemService.queryproblemById(id);
        System.out.println(problem);
        model.addAttribute("obj",problem);
        return "/problemAdd";
    }

    /**
     * 修改一个药品信息
     */
    @RequestMapping("problemEdit")
    @ResponseBody
    public Object problemEdit(Problem problem){
        try {
            int i = problemService.editproblem(problem);
            return ResultMapUtil.getMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 删除
     */
    @RequestMapping("problemDel")
    @ResponseBody
    public Object problemDel(Integer id){
        try {
            int i = problemService.delproblem(id);
            return ResultMapUtil.getMapDel(id);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

}
